﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Cinser.DataTranslate
{
    public class SdeTranslateDal:TranslateDalBase
    {
        Cinser.Common.ArcGIS.DAL.SdeDalBase dal;
        Cinser.Common.ArcGIS.DAL.SdeSettings sdeSettings;
        public SdeTranslateDal(string DbName, string UserName, string Password, string ServerIp = "127.0.0.1", string ServerName = "5151", string Version = "sde.DEFAULT")
        {
            sdeSettings = new Cinser.Common.ArcGIS.DAL.SdeSettings();
            sdeSettings.DbName = DbName;
            sdeSettings.Password = Password;
            sdeSettings.ServerIp = ServerIp;
            sdeSettings.ServerName = ServerName;
            sdeSettings.UserName = UserName;
            sdeSettings.Version = Version;
            dal = new Cinser.Common.ArcGIS.DAL.SdeDalBase(sdeSettings);
        }

        public override System.Data.DataTable GetDataList(string tbName, string sqlWhere = "1=1")
        {
            dal = new Cinser.Common.ArcGIS.DAL.SdeDalBase(sdeSettings, tbName);
            return dal.GetData(sqlWhere);
        }

        public override bool Add(string tbName, System.Data.DataRow dr)
        {
            dal = new Cinser.Common.ArcGIS.DAL.SdeDalBase(sdeSettings, tbName);
            Dictionary<string, object> columnValueMapping = new Dictionary<string, object>();
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                columnValueMapping.Add(dr.Table.Columns[i].ColumnName, dr[dr.Table.Columns[i].ColumnName]);
            }
            dal.Add(columnValueMapping);
            return true;
        }

        public bool Add(string tbName, System.Data.DataRow dr, string[] keysColumns, double x, double y)
        {
            dal = new Cinser.Common.ArcGIS.DAL.SdeDalBase(sdeSettings, tbName);
            Dictionary<string, object> columnValueMapping = new Dictionary<string, object>();
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                columnValueMapping.Add(dr.Table.Columns[i].ColumnName, dr[dr.Table.Columns[i].ColumnName]);
            }

            if (dal.GetCount(GetSqlWhere(dr, keysColumns)) > 0)
            {
                dal.Update(columnValueMapping, GetSqlWhere(dr, keysColumns), x, y);
            }
            else
            {
                dal.Add(columnValueMapping, x, y);
            }
            return true;
        }

        public override bool Add(string tbName, System.Data.DataRow dr, string[] keysColumns)
        {
            return Add(tbName, dr, keysColumns, double.Parse(dr["X"].ToString()), double.Parse(dr["Y"].ToString()));
        }

        public override bool Update(string tbName, System.Data.DataRow dr, string[] keysColumns, string[] keyValues)
        {
            throw new NotImplementedException();
        }

        public override List<string> GetAllTableNames()
        {
            return dal.dbhelper.GetTableNames();
        }

        public override bool IsExistTable(string tbName)
        {
            return dal.dbhelper.IsExistTable(tbName);
        }

        private string GetSqlWhere(DataRow dr, string[] keysColumns)
        {
            return Common.DataTableExtension.GetSqlWhere(dr, keysColumns.ToList());
        }

        public override List<string> GetColumns(string tbName)
        {
            List<string> list = new List<string>();
            DataTable dt = dal.dbhelper.GetTableInfo(tbName);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(dt.Rows[i]["COLUMN_NAME"].ToString());
                }
            }
            return list;
        }
    }
}
